package com.mlnx.web.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.device.entity.Device;
import com.mlnx.device.pojo.dto.SugarDeviceDto;
import com.mlnx.device.pojo.form.SugarDeviceFilterForm;
import com.mlnx.device.pojo.form.SugarDeviceForm;
import com.mlnx.device.service.DeviceService;
import com.mlnx.device.service.SugarDeviceService;
import com.mlnx.service.common.enums.ResponseEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Api(value = "血糖设备接口", tags = {"血糖设备接口"})
@RestController
@RequestMapping("device/sugar")
public class SugarDeviceController extends BaseController {

    @Autowired
    private SugarDeviceService sugarDeviceService;

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "血糖设备")
    @PostMapping(value = "")
    public Response addSugarDevice(@Valid SugarDeviceForm sugarDeviceForm) {
        Device device = deviceService.getOne(new QueryWrapper<Device>().eq("numeration", sugarDeviceForm.getNumeration()));
        if (device != null)
            if (device.getStatus() == 1)
                deviceService.removeById(device.getId());
            else
                return result(ResponseEnum.DeviceExisted);

        sugarDeviceService.addSugarDevice(sugarDeviceForm);

        return result();
    }


    @ApiOperation(value = "分页获取血糖设备", notes = "")
    @GetMapping(value = "")
    public ResponseData<Page<SugarDeviceDto>> list(@Valid PageForm pageForm, SugarDeviceFilterForm sugarDeviceFilterForm) {

        return result(sugarDeviceService.list(pageForm, sugarDeviceFilterForm));
    }


    @ApiOperation(value = "批量删除血糖设备", notes = "")
    @DeleteMapping(value = "")
    public Response deleteSugarDevices(Integer[] ids) {

        deviceService.deleteDevices(ids);

        return result();
    }

    @ApiOperation(value = "修改血糖设备信息", notes = "")
    @PutMapping(value = "/{id}")
    public Response editSugarDevice(@PathVariable("id") Integer id, @Valid SugarDeviceForm sugarDeviceForm) {

        sugarDeviceService.updateSugarDevice(id, sugarDeviceForm);

        return result();
    }
}
